package com.example.spritedto;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.example.common.Constants;

import android.content.Context;


public class TankDTO implements InterfaceSprite{

	private Texture tankTexture;
	private TiledTextureRegion tankRegion;
	public AnimatedSprite tankSprite;
	private int state_tank = Constants.STATE_STOP;
	private PhysicsHandler tankHandler;
	private float positionX, positionY;
	public float pX, pY;
	
	public TankDTO(float x, float y){
		this.positionX = x;
		this.positionY = y;
	}
	
	@Override
	public void onLoadResources(Engine mEngine, Context mContext) {
		TextureRegionFactory.setAssetBasePath("gfx/sprite/");
		tankTexture = new Texture(256, 32, TextureOptions.BILINEAR);
		tankRegion = TextureRegionFactory.createTiledFromAsset(tankTexture, mContext, "tank.png", 0, 0, 8, 1);
		mEngine.getTextureManager().loadTexture(tankTexture);
	}

	@Override
	public void onLoadScene(Scene scene) {
		tankSprite = new AnimatedSprite(positionX, positionY, tankRegion);
		tankHandler = new PhysicsHandler(tankSprite);
		tankSprite.registerUpdateHandler(tankHandler);
		scene.attachChild(tankSprite);
	}
	
	public void updateStatus(float pValueX, float pValueY){
		if(pValueX != 0 || pValueY != 0){
			pX = 0;
			pY = 0;
			pX = tankSprite.getX() + 16;
			pY = tankSprite.getY() + 16;
			if(pValueX > 0){
				if(state_tank == Constants.STATE_STOP){
					tankSprite.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
					tankSprite.setRotation(0);
					state_tank = Constants.STATE_RUN;
				}else if(state_tank == Constants.STATE_RUN){
					tankSprite.setRotation(0);
				}
				pX = tankSprite.getX() + tankSprite.getWidth();
				pY = tankSprite.getY() + tankSprite.getHeight()/2;
			}else if(pValueX < 0){
				if(state_tank == Constants.STATE_STOP){
					tankSprite.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
					tankSprite.setRotation(180);
					state_tank = Constants.STATE_RUN;
				}else if(state_tank == Constants.STATE_RUN){
					tankSprite.setRotation(180);
				}
				pX = tankSprite.getX();
				pY = tankSprite.getY() + tankSprite.getHeight()/2;
			}else if(pValueY < 0){
				if(state_tank == Constants.STATE_STOP){
					tankSprite.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
					tankSprite.setRotation(270);
					state_tank = Constants.STATE_RUN;
				}else if(state_tank == Constants.STATE_RUN){
					tankSprite.setRotation(270);
				}
				pX = tankSprite.getX() + tankSprite.getWidth()/2;
				pY = tankSprite.getY();
			}else if((pValueY > 0)){
				if(state_tank == Constants.STATE_STOP){
					tankSprite.animate(new long[]{200, 200, 200, 200, 200, 200, 200}, 1, 7, true);
					tankSprite.setRotation(90);
					state_tank = Constants.STATE_RUN;
				}else if(state_tank == Constants.STATE_RUN){
					tankSprite.setRotation(90);
				}
				pX = tankSprite.getX() + tankSprite.getWidth()/2;
				pY = tankSprite.getY() + tankSprite.getHeight();
			}
			moveRelativeXY(pValueX, pValueY);
		}else{
			tankSprite.animate(new long[]{200}, new int[]{0}, 100000);
			state_tank = Constants.STATE_STOP;
			moveRelativeXY(0, 0);
		}
	}
	
	public void moveRelativeXY(float pValueX, float pValueY){
		tankHandler.setVelocity(pValueX*50, pValueY*50);
	}
	
	public float getPX(){
		return pX;
	}
	
	public float getPY(){
		return pY;
	}
}
